home *** CD-ROM | disk | FTP | other *** search
/ ftp.mactech.com 2010 / ftp.mactech.com.tar / ftp.mactech.com / machack / Hacks97 / WhackABill.sit / Whack-A-Bill / source code / aronsgame.c < prev    next >
C/C++ Source or Header  |  1997-09-24  |  6KB  |  335 lines

  1. #include <stdio.h>
  2. #include <math.h>
  3. #include <qdoffscreen.h>
  4. #include <sound.h>
  5. #include "draw.h"
  6. #include "async.h"
  7. #include "hidembar.h"
  8. #include "score.h"
  9. #include "health.h"
  10.  
  11. void InitMac(void);
  12. void WindowSetup(void);
  13. void Redraw(void);
  14. void doEvent(void);
  15. void SetDrawState(void);
  16. void SetPlayerState(void);
  17. void IncreaseScore(void);
  18. void GameOff(void);
  19. void GameOn(void);
  20.  
  21.  
  22. Boolean         gDone;
  23. long lastRedraw;
  24. long playerOneUpdateTime;
  25. long playerTwoUpdateTime;
  26. int drawState;
  27. Boolean background;
  28. Boolean soundPlayed;
  29. char lastCharacter;
  30. long PlayerOneScore;
  31. long PlayerTwoScore;
  32. Boolean gameOn=true;
  33. Boolean gatesBlood=false;
  34. Boolean aronBlood=false;
  35. PicHandle splashScreen;
  36. Rect gdestrect;
  37. Boolean splashOn=false;
  38.  
  39.  
  40. WindowPtr gMainWindow;
  41.  
  42.  
  43. EventRecord        gTheEvent;
  44.  
  45. main()
  46. {
  47. KeyMap keyMap;
  48. Rect sourceRect;
  49. Rect destRect;
  50.  
  51. int eventMask;
  52. EventRecord theEvent;
  53. EventRecord bufferEvent;
  54. int sleep;
  55. RgnHandle mouseRgn=NewRgn();
  56. Boolean gotEvent;
  57.  
  58. sleep = 50;
  59. eventMask = 78;
  60.  
  61. InitMac();
  62. WindowSetup();
  63. setupGWorld();
  64.  
  65. pl1info = (player **)NewHandleClear( sizeof( player ));
  66. pl2info = (player **)NewHandleClear( sizeof( player ));
  67.  
  68. LoadImages();
  69. //splashScreen=GetPicture(5000);
  70.  
  71.  
  72. SetRect(&sourceRect,320,210,640,420);
  73. SetRect(&destRect,0,0,320,210);
  74.  
  75. CopyPixels(sourceRect,destRect);
  76. SetupSound();
  77. updateScores(PlayerOneScore,PlayerTwoScore);
  78. UpdateHealth();
  79. GameOff();
  80. //splashScreen=GetPicture(5000);
  81.  
  82. while (!gDone)
  83. {
  84. gotEvent = WaitNextEvent(everyEvent,&gTheEvent,sleep,mouseRgn);
  85. if (gotEvent)
  86.     doEvent();
  87. SetPlayerState();
  88. SetDrawState();
  89.     if(gameOn)
  90.     {
  91. Redraw();
  92. IncreaseScore();
  93. PlayAsync(drawState);
  94. SetHealth(PlayerOneScore,PlayerTwoScore);
  95.     }
  96. }
  97.  
  98. DisposeHandle( (Handle) pl1info );
  99. DisposeHandle( (Handle) pl2info );
  100. DisposeSound();
  101.  
  102. //ShowMenuBar();
  103. InitMac();
  104. }
  105.  
  106. void doEvent(void)
  107. {
  108.  
  109.     switch (gTheEvent.what)
  110.     {
  111. //        case mouseDown:
  112. //            DoMouseDown();
  113. //            break;
  114.         case keyDown:
  115.             lastCharacter = gTheEvent.message & charCodeMask;
  116.  
  117.  
  118.             if (splashOn)
  119.             if ((lastCharacter == 'Q') || (lastCharacter == 'q'))
  120.                 if (gTheEvent.modifiers & cmdKey)
  121.                     gDone=true;
  122.  
  123.             if ((lastCharacter == 'Q') || (lastCharacter == 'q'))
  124.                 if (gTheEvent.modifiers & cmdKey)
  125.                     DrawPicture(splashScreen, &gdestrect);
  126.                     splashOn=true;
  127.                     
  128.  
  129.  
  130.  
  131.             if (lastCharacter == ' ')
  132.             {
  133.                 if(gameOn)
  134.                     GameOff();
  135.                 else
  136.                     GameOn();
  137.             }
  138.             break;
  139.         case osEvt:
  140.     
  141.             switch (gTheEvent.what )
  142.                     {
  143.                     case activateEvt:
  144.                           background = ( (gTheEvent.modifiers & activeFlag) != 0 );
  145.                           break;
  146.                     }
  147.             break;
  148.                 
  149.                 }
  150. }
  151.  
  152. void SetPlayerState(void)
  153. {
  154. long tick;
  155. KeyMap keyMap;
  156. int rand;
  157.  
  158. tick=TickCount();
  159.  
  160.     if((playerOneUpdateTime+5)<tick)
  161.             {
  162.             playerOne.state=1;
  163.             }
  164.     if((playerTwoUpdateTime+5)<tick)
  165.             {
  166.             playerTwo.state=1;
  167.             }
  168.             
  169.             if (((lastCharacter == 'D')||(lastCharacter=='d'))&&((playerOneUpdateTime+10)<tick))
  170.                 {
  171.                 playerOne.state=0;
  172.                 playerOneUpdateTime=tick;
  173.                 soundPlayed=false;
  174.                 lastCharacter = '~';
  175.                 }
  176.  
  177.             if (((lastCharacter == 'K')||(lastCharacter=='k'))&&((playerTwoUpdateTime+10)<tick))
  178.                 {
  179.                 playerTwoUpdateTime=tick;
  180.                 playerTwo.state=0;
  181.  
  182.                 soundPlayed=false;
  183.                 lastCharacter = '~';
  184.                 }
  185.         
  186.                 GetKeys(keyMap);
  187.                 if((keyMap[0] >> 16) & 0x01)
  188.                     playerOne.state=2;
  189.                 if((keyMap[1] >> 22) & 0x01)
  190.                     playerTwo.state=2;
  191.                     
  192.                 if(playerOne.state==0)
  193.                 if((keyMap[1] >> 16) & 0x01)
  194.                     {
  195.                     rand = (Random()%100);
  196.                     if (rand<50)
  197.                         playerOne.state=1;
  198.                     if (rand>50)
  199.                         playerTwo.state=1;
  200.                     }
  201.                     
  202.                 if(playerTwo.state==0)
  203.                 if((keyMap[0] >> 26) & 0x01)
  204.                     {
  205.                     rand = (Random()%100);
  206.                     if (rand<50)
  207.                         playerOne.state=1;
  208.                     if (rand>50)
  209.                         playerTwo.state=1;
  210.                     }
  211.                 
  212.             
  213.                 SetDrawState();        
  214.                 
  215. }
  216.  
  217. void SetDrawState(void)
  218. {
  219.             if((playerOne.state==0)&&(playerTwo.state==1))
  220.                 drawState=1;
  221.             if((playerOne.state==0)&&(playerTwo.state==2))
  222.                 drawState=2;
  223.             if((playerOne.state==1)&&(playerTwo.state==0))
  224.                 drawState=3;
  225.             if((playerOne.state==1)&&(playerTwo.state==1))
  226.                 drawState=4;
  227.             if((playerOne.state==1)&&(playerTwo.state==2))
  228.                 drawState=5;
  229.             if((playerOne.state==2)&&(playerTwo.state==0))
  230.                 drawState=6;
  231.             if((playerOne.state==2)&&(playerTwo.state==1))
  232.                 drawState=7;
  233.             if((playerOne.state==2)&&(playerTwo.state==2))
  234.                 drawState=8;
  235. }
  236.  
  237. void Redraw(void)
  238. {
  239. DrawBackground();
  240. DrawPlayers(drawState, soundPlayed);
  241. DrawFinal();
  242. lastRedraw=TickCount();
  243. }
  244.  
  245. void InitMac (void)
  246. {
  247.     InitGraf (&qd.thePort);
  248.     InitFonts ();
  249.     FlushEvents (everyEvent, 0);
  250.     InitWindows ();
  251.     InitMenus ();
  252.     TEInit ();
  253.     InitDialogs (0L);
  254.     InitCursor ();
  255.     MaxApplZone ();
  256.     
  257. //    HideCursor();
  258.     HideMenuBar();
  259.     
  260. }
  261.  
  262. void WindowSetup (void)
  263. {
  264.     Rect scoreWindowRect;
  265.     Rect screenRect;
  266.     Rect mainWindowRect;
  267.     GDHandle hGD;
  268.     PicHandle background;
  269.     Rect backgroundRect;
  270.     
  271.     hGD=GetMainDevice();
  272.     screenRect=((GDPtr)*hGD)->gdRect;
  273.     SetRect(&mainWindowRect, (screenRect.right/2)-320, (screenRect.bottom/2)-240, (screenRect.right/2)+320, (screenRect.bottom/2)+240);
  274.     SetRect(&backgroundRect,0,0,640,480);
  275.     
  276.     gScreenWindow = NewCWindow (0L, &screenRect, "\p ", true, plainDBox, (WindowPtr) - 1L, true, 0);
  277.     PaintRect(&screenRect);
  278.     background=GetPicture(600);
  279.     
  280.     gMainWindow = NewCWindow (0L, &mainWindowRect, "\p ", true, plainDBox, (WindowPtr) - 1L, true, 0);
  281.     SetPort(gMainWindow);
  282.     DrawPicture(background,&backgroundRect);
  283.  
  284.     SetRect (&gWindowTwoRect, mainWindowRect.left+62,mainWindowRect.top+107,mainWindowRect.left+382,mainWindowRect.top+317);
  285.     gWindowTwo = NewCWindow (0L, &gWindowTwoRect, "\pThe Perfect Game. Well, a Game", true, plainDBox, (WindowPtr) - 1L, true, 0);
  286.  
  287. }
  288.  
  289. void IncreaseScore(void)
  290. {
  291. if (!(drawState==lastSoundPlayed))
  292. {
  293. switch (drawState)
  294. {
  295. case 1:
  296.     PlayerOneScore++;
  297.     break;
  298. case 2:
  299.     break;
  300. case 3:
  301.     PlayerTwoScore++;
  302.     break;
  303. case 4:
  304.     break;
  305. case 5:
  306.     break;
  307. case 6:
  308.     break;
  309. case 7:
  310.     break;
  311. case 8:
  312.     break;
  313. }
  314. updateScores(PlayerOneScore,PlayerTwoScore);
  315. }
  316. }
  317.  
  318. void GameOff(void)
  319. {
  320. Rect destRect;
  321. PicHandle eyesPic;
  322.  
  323. eyesPic=GetPicture(4711);
  324. SetRect(&destRect,0,0,320,240);
  325. SetPort(gWindowTwo);
  326. DrawPicture(eyesPic,&destRect);
  327. gameOn=false;
  328. }
  329.  
  330. void GameOn(void)
  331. {
  332. gameOn=true;
  333. }
  334.  
  335.